﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.VisualBasic.ApplicationServices;
using System.IO;
using System.Threading;
using AutoDWF;
using AutoDWF.NET.Util;
using AutoDWF.Forms;

namespace AutoPublish
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new MainForm());
            //单实例管理器
            SingleInstanceManager manager = new SingleInstanceManager();
            manager.Run(new string[] { });
        }
    }

    // Using VB bits to detect single instances and process accordingly:
    // * OnStartup is fired when the first instance loads
    // * OnStartupNextInstance is fired when the application is re-run again
    //    NOTE: it is redirected to this instance thanks to IsSingleInstance
    public class SingleInstanceManager : WindowsFormsApplicationBase
    {
        MainForm app;


        public SingleInstanceManager()
        {
            this.IsSingleInstance = true;
        }

        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
        {
            try
            {
                //自动登录
                //处理是否注册OCX控件，不显示命令行窗口
                try
                {
                    string language = IniHelper.IniReadValue("System", "Language", IniHelper.configFilePath);
                    if (string.IsNullOrEmpty(language))
                    {
                        LanguageForm lf = new LanguageForm();
                        lf.ShowDialog();
                    }
                    else
                    {
                        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(language);
                        Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);
                    }

                    app = new MainForm();
                }
                catch (System.Runtime.InteropServices.COMException ex)
                {
                    RegsvrHelper.RegsvrDWGThumbnail();
                    Thread.Sleep(2000);
                    app = new MainForm();
                }

                Application.Run(app);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString(), "系统消息", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            

            return false;
        }

        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
            // Subsequent launches
            base.OnStartupNextInstance(eventArgs);
            app.ShowInTaskbar = true;
            app.Activate();
            app.Show();
        }
    }

}